package io.renren.common.aliPay;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import io.renren.common.enums.ErrorCode;
import io.renren.common.exception.RRException;
import io.renren.common.utils.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@Slf4j
public class AliPayHelper {

    @Autowired
    private AlipayClient alipayClient;

    @Value("${baseConstant.aliPay.notifyUrl}")
    private String notifyUrl;

    /**
     * @author Lee
     * @description 请求支付
     * @date 2020-02-25 18:27
     **/
    public String pay(AlipayTradeAppPayModel model) {
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            log.info("支付宝下单:【{}】", response.getBody());
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
            throw new RRException(e.getMessage(),400);
        }
    }

    /**
     * @author Lee
     * @description 交易查询
     * @date 2020-02-25 20:41
     **/
    public AlipayTradeQueryResponse orderQuery(String orderCode) {
        try {
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//创建API对应的request类
            request.setBizContent("{" +
                    "   \"out_trade_no\":\"" + orderCode + "\"" +
                    "  }");//设置业务参数
            AlipayTradeQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API，获得对应的response类
            log.info("支付宝订单查询:【{}】", response.getBody());
            if (!"10000".equals(response.getCode())) {
                throw new RRException(response.getSubMsg(),Integer.valueOf(response.getCode()));
            }
//            response.getTradeStatus().equals("TRADE_SUCCESS")
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(ErrorCode.ERROR.getMessage(),ErrorCode.ERROR.getCode());
        }
    }


    /**
     * @author Lee
     * @description 退款
     * @date 2020-02-25 21:21
     **/
    public AlipayTradeRefundResponse refund(String orderCode, BigDecimal amount) {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        try {
            request.setBizContent("{" +
                    "    \"out_trade_no\":\"" + orderCode + "\"," +
//                "    \"trade_no\":\"2014112611001004680073956707\"," +
                    "    \"out_request_no\":\"" + SnowflakeIdWorker.getNextOrderNo() + "\"," +
                    "    \"refund_amount\":\"" + amount.doubleValue() + "\"" +
                    "  }");//设置业务参数
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            log.info("支付宝退款:【{}】", response.getBody());
            if (!"10000".equals(response.getCode())) {
                throw new RRException(response.getSubMsg(),Integer.valueOf(response.getCode()));
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(ErrorCode.ERROR.getMessage(),ErrorCode.ERROR.getCode());
        }
    }
}
